package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.qo.EmployeeQueryObject;
import cn.wolfcode.rbac.qo.PageResult;
import cn.wolfcode.rbac.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;

	@Override
	public List<Employee> selectList(EmployeeQueryObject qo) {
		return employeeMapper.selectList(qo);
	}

	@Override
	public PageResult<Employee> selectPage(EmployeeQueryObject qo) {
		PageResult<Employee> pageResult = null;
		int totalCount = employeeMapper.selectCount(qo);
		if (totalCount == 0){
			pageResult = new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
					0,new ArrayList<>());
		}else{
			List<Employee> employeeList = employeeMapper.selectList(qo);
			pageResult = new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),
					totalCount,employeeList);
		}
		return pageResult;
	}

	@Override
	public Employee selectOne(Long id) {

		return employeeMapper.selectOne(id);
	}

	@Override
	public List<Role> seleteRolesByEmployeeId(Long id) {
		return employeeMapper.seleteRolesByEmployeeId(id);
	}

	@Override
	public void UpdateByEmployeeId(Employee employee) {
		if(employee.getAdmin()==null)
			employee.setAdmin(false);
		employeeMapper.UpdateByEmployeeId(employee);
	}

	@Override
	public List<String> selectPermissonByEmployeeId(Long id) {
		return employeeMapper.selectPermissonByEmployeeId(id);
	}


	@Override
	public void insertRelationRoleIdAndEmployeeId(Long employeeId, Long[] ids) {
		employeeMapper.deleteRelationRole(employeeId);
		for (int i=0;i< ids.length;i++)
			employeeMapper.insertRelationRoleIdAndEmployeeId(employeeId,ids[i]);

	}

	@Override
	public Employee selectByName(String name,String password) {
		Employee employee= employeeMapper.selectByName(name);
//		if(employee.getName().equals(name)&&employee.getPassword().equals(password))
			return employee;
//		return null;
	}

    @Override
    public void insertEmployee(Employee employee) {
        employeeMapper.insertEmployee(employee);
    }

	@Override
	public void deleteById(Long id) {
		employeeMapper.deleteRelationRole(id);
		employeeMapper.deleteById(id);
	}


}
